<?php
namespace Cyfy\Modules\Menu;

use \Cyfy\Modules\Menu\Module;
/**
 * Administration for menus.
 *
 * @version 0.1
 * @author Dandelion
 */ 
class Administration extends \Cyfy\Translatable
{
	
	static function menus()
	{
        // loads UI
		\Cyfy\Modules\jquery\Plugins :: load("jqueryUI");
        \Cyfy\Cyfy ::  javascript(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Menu/admin.js"), "file");
        \Cyfy\Cyfy ::  css(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Menu/admin.css"), "file");
        
        // loads menus
        $result = \dibi :: query("SELECT [mid], [name], [code] FROM [:cyfy:Menu-menus]");
        $menus = $result -> fetchPairs('mid', 'name');
        
		// reset mysqli counter
        $result -> seek(0);
        
        // print menus
        foreach($result as $m)
		{
			//buttons
            $deleteButton = "<input type=\"submit\" class=\"button-warning\" name=\"\" onclick=\"if(confirm('". self :: t(120, array($m["name"]))."')){ Menu.deleteMenu({$m["mid"]}) }\" value=\"" . self::t(110) . "\" />";
            $saveButton = "<input type=\"submit\" name=\"\" value=\"" . self :: t(130) . "\" onclick=\"Menu.submit({$m["mid"]}, '{$m["code"]}')\" />";
            $addButton = "<input type=\"submit\" class=\"button-neutral\" name=\"\" value=\"" .  self :: t(140) . "\" onclick=\"Menu.addItem({$m["mid"]})\" />";
			
	        $loadedMenu = self :: getMenu($m["mid"]);
   		
            $output .= <<<OUTPUT
<div id="menuAdmin-{$m["mid"]}">
	<h3>{$m["name"]} [{$m["code"]}]</h3>
	
	<div id="menuTableDiv-{$m["mid"]}" class="menuTableDiv">
	$loadedMenu
	</div>

	<div class="form-buttons" style="padding-top:0px">
		$saveButton
		$addButton
		$deleteButton
	</div>

</div>
OUTPUT;
			
        }
	
		// create menu form
		$form = new Forms\Create();
		$output .= "<h2>" . self :: t(150) . "</h2>\n";
		$output .= $form -> show();
		
        return $output;
    }
	
    /**
     * Deletes menu
     * @param int Menu ID
     * @return string
     */
	static function delete($mid)
	{
        \dibi :: query("DELETE FROM [:cyfy:Menu-menus] WHERE [mid] = %i", $mid);
        \dibi :: query("DELETE FROM [:cyfy:Menu-items] WHERE [mid] = %i", $mid);
			
        \Cyfy\Message :: set(self :: t(210));
		
		return \Cyfy\Message :: get(self :: t(220), \Cyfy\Message::WARNING);
    }
	
    /**
     * Saves menu
     * @param int Menu ID
     * @return bool
     */
	static function save($mid)
	{
   
        foreach($_POST["mis"] as $mi)
		{
			// if new
			if(strpos($mi, "NEW") !== false){
				if($_POST["names"][$mi] && $_POST["links"][$mi]){
					\dibi :: query("INSERT INTO [:cyfy:Menu-items] VALUES(0,'".\Cyfy\Language :: getActive()."', {$mid} , '{$_POST["names"][$mi]}', '', '{$_POST["links"][$mi]}' , {$_POST["depths"][$mi]}, {$_POST["weights"][$mi]})");
				}
			}
			// if editing
			else
			{
				// if delete
				if($_POST["delete"][$mi] == 1){
					\dibi :: query("DELETE FROM [:cyfy:Menu-items] WHERE `miid` = %i", $mi);
				}else{
				// if editing
					$updt = "";
					if($_POST["names"][$mi] || $_POST["links"][$mi]){
						$updt = "[name] = '" . $_POST["names"][$mi] . "', [link]='" . $_POST["links"][$mi] . "', ";
					}
					\dibi :: query("UPDATE [:cyfy:Menu-items] SET $updt [depth] = '{$_POST["depths"][$mi]}', [weight] = '{$_POST["weights"][$mi]}' WHERE [miid] = %i", $mi);
				}
			
			}
		}
        
        \Cyfy\Message :: set(self :: t(230));
        
		return self :: getMenu($mid);
    }
	
    /**
     * Returns a menu
     *
     * @param int Menu ID
     */	
	static private function getMenu($mid)
	{    
        $result = \dibi :: query("SELECT * FROM [:cyfy:Menu-items] WHERE [mid] = $mid AND language = %s ORDER BY [weight]", \Cyfy\Language :: getActive());
        
		// preparation for table
        $name = self :: t(30);
        $link = self :: t(40);
        $action = self :: t(50);
        
		// the table
$output = <<<TABLE
<table id="menuTable-{$mid}" class="menuTable">
    <tr>
        <th class="name">{$name}</th>
        <th class="link">{$link}</th>
        <th class="action">{$action}</th>
    </tr>
    <tbody class="items">
    
TABLE;
		// if items
		if(count($result))
		{
            foreach($result as $mi)
			{
$output .= <<<ROW

        <tr class="MI-{$mi["miid"]}">
            <td class="depth">{$mi["depth"]}</td>
            <td class="name">{$mi["name"]}</td>
            <td class="link">{$mi["link"]}</td>
            <td class="action">
                <input type="submit" value="&lt;" onclick="Menu.setDepth($mid, {$mi["miid"]}, -1);" />
                <input type="submit" value="&gt;" onclick="Menu.setDepth($mid, {$mi["miid"]}, 1)" /> &nbsp; &nbsp;
                <input type="submit" value="E" onclick="Menu.edit({$mi["miid"]});" />
                <input type="submit" class="button-warning" value="X" onclick="Menu.del({$mi["miid"]});" />
            </td>
        </tr>

ROW;
            }
        }
	
        $output .= "    </tbody>\n</table>\n";
		return $output;
    }
}
